// Guba.js for PSPTube
// Made By JK108

function Guba_CheckURL( url, option )
{
	if (url.match("http://www.guba.com/") == "http://www.guba.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Guba URL
*/
function Guba_GetURL( url, option )
{
	var pos = url.lastIndexOf("/", url.length);
	var id = url.substring(pos + 1, url.length);
	var video_url = "http://free.guba.com/uploaditem/" + String(id) + "/flash.flv";
	return video_url;
}

function Guba_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 40;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.guba.com/general/search?query=" + keyword.replace(/ /g, "+") );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://www.guba.com/general/search?query=" + keyword.replace(/ /g, "+") + "&o=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }



	if(contents.match("</span> There were no matches found for query <b>") == "</span> There were no matches found for query <b>"){ result.total = 0; return result; }
	var pos = contents.indexOf("in General Videos returned ");
	contents = contents.substring(pos + 27, contents.length);
	pos = contents.indexOf(" items");
	var total = contents.substring(0, pos).replace(/,/g, "");
	result.total = total;



	while(1){
	pos = contents.indexOf("<div id=\"block_thumb\"  class=\"video_med\"  >");

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 23, contents.length);
		pos = contents.indexOf("<a href=\"");
		contents = contents.substring(pos + 9, contents.length);
		pos = contents.indexOf("?");
		var url = "http://www.guba.com" + contents.substring(0, pos);
		//Get Thumbnail
		pos = contents.indexOf("<img src=\"");
		contents = contents.substring(pos + 10, contents.length);
		pos = contents.indexOf("\" ");
		var thumb_url = contents.substring(0, pos);
		//Title
		pos = contents.indexOf(";sample=");
		contents = contents.substring(pos + 8, contents.length);
		pos = contents.indexOf("\">");
		contents = contents.substring(pos + 2, contents.length);
		pos = contents.indexOf("</a></div>");
		var title = ImproveText(contents.substring(0, pos));
		//Length
		pos = contents.indexOf("</a></b> (");
		contents = contents.substring(pos + 10, contents.length);
		pos = contents.indexOf(")");
		var length = contents.substring(0, pos);
		var length = TimestringToSeconds(length);
		//Description
		contents = contents.substring(pos + 1, contents.length);
		if(contents.substring(0, 5) == "</li>"){
			var description = title;
		} else{
			pos = contents.indexOf("<p>");
			contents = contents.substring(pos + 3, contents.length);
			pos = contents.indexOf("</p></li>");
			var description = ImproveText(contents.substring(0, pos));
		}
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);
		//Views
		pos = contents.indexOf("<b>Views:</b> ");
		contents = contents.substring(pos + 14, contents.length);
		pos = contents.indexOf("</li>");
		var views = parseInt(contents.substring(0, pos).replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Guba = new Object();
Guba.Name          = "Guba";		/* Service name */
Guba.Description   = "Guba";		/* Service description */
Guba.SearchDesc    = "Guba";		/* Service search screen description */
Guba.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Guba.CheckURL      = Guba_CheckURL;	/* CheckURL function */
Guba.GetURL        = Guba_GetURL;	/* GetURL function */
Guba.Search        = Guba_Search;	/* Search function */

// add Guba to site list
SiteList.push( Guba );
